layui.define(["table", "form", "admin", "upload"], function (exports) {
    var $ = layui.$,
        table = layui.table,
        form = layui.form,
        upload = layui.upload,
        admin = layui.admin;

    table.render({
        elem: "#LAY-push-manage",
        url: "/admin/offline_push/index",
        cols: [
            [
                { field: "id", title: "ID", width: 70 },
                { field: "title", title: "推送标题", width: 200 },
                { field: "content", title: "推送内容", width: 300 },
                {
                    field: 'sport_id', title: '关联赛事类型', width: 150, align: "center", templet: function (item) {
                        if (!item.sport_id) {
                            return '-';
                        }

                        if (item.sport_id == 1) {
                            return '足球';
                        }

                        if (item.sport_id == 2) {
                            return '篮球';
                        }

                        return '-';
                    }
                },
                {
                    field: 'match_id', title: '关联赛事ID', width: 120, align: "center", templet: function (item) {
                        if (!item.match_id) {
                            return '-';
                        }

                        return item.match_id;
                    }
                },
                {
                    field: 'comp', title: '关联赛事名称', width: 200, templet: function (item) {
                        if (!item.comp) {
                            return '-';
                        }

                        return item.comp;
                    }
                },
                {
                    field: 'user', title: '接受用户', width: 150, align: "center", templet: function (item) {
                        if (!item.user) {
                            return '全部';
                        }

                        if (item.user == 0) {
                            return '全部';
                        }

                        return item.user;
                    }
                },
                { field: 'push_time', title: '推送时间', width: 175, align: "center", },
                { field: 'create_time', title: '创建时间', width: 175, align: "center", },
                {
                    field: 'push_status', title: '推送状态', width: 135, align: "center", templet: function (item) {
                        if (item.push_status == 2) {
                            return '已推送';
                        }

                        return '<font style="color: #f0ad4e">待推送</font>';
                    }
                },
                {
                    title: "操作",
                    width: 250,
                    align: "center",
                    toolbar: "#table-push-operation",
                },
            ],
        ],
        page: true,
        limit: 20,
        height: "full-140",
        text: "对不起，加载出现异常！",
        even: true,
    });

    //监听工具条
    table.on("tool(LAY-push-manage)", function (obj) {
        if (obj.event === "edit") {
            layer.open({
                type: 2,
                title: ["编辑", 'font-size:16px'],
                content: '/admin/offline_push/edit?id=' + obj.data.id,
                area: ['700px', '665px'],
                //offset: '0px',
            });
        }
        if (obj.event == "del") {
            layer.confirm('确认要删除吗？', function (obj) {
                $.ajax({
                    type: "POST",//请求方式
                    url: '/admin/offline_push/del', //请求地址
                    data: { "id": obj.data.id },
                    dataType: "json",//返回的数据类型
                    //请求成功
                    success: function (data) {
                        if (data.code == 0) {
                            layer.msg(data.msg, { icon: 1, time: 2000 }, function () {
                                window.location.reload();
                            });
                        } else {
                            layer.msg(data.msg, { icon: 2 });
                        }
                    },
                    //请求失败，包含具体的错误信息
                    error: function (e) {
                        if (e.responseJSON.msg) {
                            layer.msg(e.responseJSON.msg, { icon: 2 });
                        }
                    }
                });
            });
        }

        if (obj.event == "push") {
            layer.confirm('确认立即推送吗？', function (obj) {
                $.ajax({
                    type: "POST",//请求方式
                    url: '/admin/offline_push/push', //请求地址
                    data: { "id": obj.data.id },
                    dataType: "json",//返回的数据类型
                    //请求成功
                    success: function (data) {
                        if (data.code == 0) {
                            layer.msg(data.msg, { icon: 1, time: 2000 }, function () {
                                window.location.reload();
                            });
                        } else {
                            layer.msg(data.msg, { icon: 2 });
                        }
                    },
                    //请求失败，包含具体的错误信息
                    error: function (e) {
                        if (e.responseJSON.msg) {
                            layer.msg(e.responseJSON.msg, { icon: 2 });
                        }
                    }
                });
            });
        }
    });
    exports("offline_push", {});
});
